package BryantChad.LunchTimeScoreCard;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class KeepScoreActivity  extends ListActivity {
	private Game curGame_;
	private PlayerAdapter playerAdapter_;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keepscore);

        Intent intent = getIntent();
        curGame_ = (Game)intent.getSerializableExtra("game");
        
        playerAdapter_ = new PlayerAdapter(this, R.layout.gameitem, curGame_.Players);
        setListAdapter(playerAdapter_);

        final Button btnNewGame = (Button) findViewById(R.id.btnStartOver);
        btnNewGame.setOnClickListener(btnStartOverClick);

        final Button btnResetScore = (Button) findViewById(R.id.btnResetScore);
        btnResetScore.setOnClickListener(btnResetScoreClick);
        
    }
	
	public void btnAddScoreClick(View v) {
		//get the row the clicked button is in
		Player playerToChange = (Player) v.getTag();
		LinearLayout vwParentRow = (LinearLayout)v.getParent();
		TextView lblScore = (TextView) vwParentRow.getChildAt(1);
        EditText txtScore = (EditText) vwParentRow.getChildAt(2);
        int handScore;
		
		try {
			handScore = Integer.parseInt(txtScore.getText().toString());
		} catch(NumberFormatException nfe) {
			Toast.makeText(this, "The score entered is not a number", Toast.LENGTH_LONG).show();
			handScore = 0;
		}
        
		playerToChange.addToScore(handScore);
		lblScore.setText(Integer.toString(playerToChange.getScore()));
		txtScore.setText("");
				
        //vwParentRow.refreshDrawableState(); //redraw
	}
	
	private class PlayerAdapter extends ArrayAdapter<Player> {

        private ArrayList<Player> players;

        public PlayerAdapter(Context context, int textViewResourceId, ArrayList<Player> players) {
                super(context, textViewResourceId, players);
                this.players = players;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.gameitem, null);
                }
                Player o = players.get(position);
                if (o != null) {
                        TextView name = (TextView) v.findViewById(R.id.lblPlayerName);
                        TextView score = (TextView) v.findViewById(R.id.lblPlayerScore);
                        Button add = (Button) v.findViewById(R.id.btnAddScore);
                        if (name != null) {
                        	name.setText(o.getName());
                        }
                        if(score != null){
                        	score.setText(Integer.toString(o.getScore()));
                        }
                        if(add != null){
                        	add.setTag(o);
                        }
                }
                return v;
        }
	}
	
    private View.OnClickListener btnStartOverClick = new View.OnClickListener() {
    	@Override
		public void onClick(View v)  {
            // Perform action on clicks
    		Intent myIntent = new Intent(KeepScoreActivity.this, LunchTimeScoreCardActivity.class);
    		
    		KeepScoreActivity.this.startActivity(myIntent);
        }
	};

    private View.OnClickListener btnResetScoreClick = new View.OnClickListener() {
    	@Override
		public void onClick(View v)  {
            // Perform action on clicks
    		for(int curPlayer=0; curPlayer<curGame_.Players.size(); curPlayer++) {
    			curGame_.Players.get(curPlayer).setScore(0);			
    		}
    		
    		playerAdapter_ = new PlayerAdapter(KeepScoreActivity.this, R.layout.gameitem, curGame_.Players);
            setListAdapter(playerAdapter_);
        }
	};
	
}
